package com.nnggstory.logs.logsocket;

import java.text.DateFormat;
import java.util.Calendar;

import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.StackTraceElementProxy;

public class LogSLogModel {
	private static DateFormat df = DateFormat.getInstance();
	private static Calendar cal = Calendar.getInstance();
	
	private String time = null;
	private String location = null;
	private String throwsInfo = null;
	private String message = null;
	
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public String getThrowsInfo() {
		return throwsInfo;
	}
	public void setThrowsInfo(String throwsInfo) {
		this.throwsInfo = throwsInfo;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	public void setLogsInfo(ILoggingEvent logBackEvent) {
		cal.setTimeInMillis(logBackEvent.getTimeStamp());
		String time = df.format(cal.getTime());
		
		//location
		StackTraceElement[] steArray = logBackEvent.getCallerData();
		String location = "";
		for (int i = 0; steArray != null && i < steArray.length; i++) {
			StackTraceElement locationInfo = steArray[i];
			location = locationInfo.getClassName()+"["+locationInfo.getMethodName()+":"+locationInfo.getLineNumber()+ "]";// + locationInfo.getClassName();
		}
		
		String throwsString = "";
		StackTraceElementProxy[] stepArray = null;
		if(logBackEvent.getThrowableProxy() != null) {
			stepArray = logBackEvent.getThrowableProxy().getStackTraceElementProxyArray();
			for (int i = 0; stepArray != null && i < stepArray.length; i++) {
				throwsString += "\n" + stepArray[i].getSTEAsString();
			}
		}
		setLocation(location);
		setTime(time);
		setThrowsInfo(throwsString);
		setMessage(logBackEvent.getMessage());
	}
	
	public void setLogsInfo(LoggingEvent log4jEvent) {
		cal.setTimeInMillis(log4jEvent.getTimeStamp());
		String time = df.format(cal.getTime());
		
		//location
		LocationInfo locationInfo = log4jEvent.getLocationInformation();
		String location = locationInfo.getClassName()+"["+locationInfo.getMethodName()+":"+locationInfo.getLineNumber()+ "]";// + locationInfo.getClassName();
		
		//ThrowableInfo
		String[] throwsStringArray = log4jEvent.getThrowableStrRep();
		String throwsString = "";
		for (int i = 0; throwsStringArray != null && i < throwsStringArray.length; i++) {
			throwsString += "\n" + throwsStringArray[i];
		}
		
		setLocation(location);
		setTime(time);
		setThrowsInfo(throwsString);
		setMessage(String.valueOf(log4jEvent.getMessage()));
	}
	@Override
	public String toString() {
		String ret = getTime() + " " + getLocation() + " " + getMessage();
		if(getThrowsInfo() != null && !"".equals(getThrowsInfo())) {
			ret += getThrowsInfo();
		}
		return ret;
	}
	
	
	
}
